﻿namespace NorthernLights
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// DebugLogger class.
    /// </summary>
    public class DebugLogger : ILogger
    {
        #region Fields
        private static DebugLogger logger = new DebugLogger();
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes static members of the DebugLogger class.
        /// </summary>
        static DebugLogger()
        {
        }

        /// <summary>
        /// Prevents a default instance of the DebugLogger class from being created.
        /// </summary>
        private DebugLogger()
        {
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets an instance of the DebugLogger class.
        /// </summary>
        public static DebugLogger Instance
        {
            get
            {
                return logger;
            }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Writes an message to the debug logger.
        /// </summary>
        /// <param name="format">The formatted string.</param>
        /// <param name="args">The arguments.</param>
        public override void Write(string format, params object[] args)
        {
#if DEBUG
            StackTrace st = new StackTrace();

            if (st != null && st.FrameCount > 1)
            {
                var method = st.GetFrame(1).GetMethod();

                if (method != null && method.ReflectedType != null)
                {
                    Debug.WriteLine("DEBUG: Calling from: " + method.Name + " within " + method.ReflectedType.Name);
                }
            }
#endif

            Debug.WriteLine(string.Format("DEBUG: " + format, args));
        }

        /// <summary>
        /// Writes an message to the debug logger.
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        public void Write(Exception ex)
        {
            this.Write("Exception: {0}{1}InnerException:{1}{2}{1}Stacktrace: {3}{1}", ex.Message, Environment.NewLine, ex.InnerException, ex.StackTrace);
        }
        #endregion
    }
}
